<?php
class Smanual extends Ecore_Db {
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache = new Ecore_Cache();
		$this->clear = $this->cache->clear;
	}
	public function getAll($intAdType,$intStatus, $intOption, $strKey, $strStart, $strEnd, $intOff, $intLimit, &$intTotal) {
		try {						
			$this->prepare('call smanuals_getall_a(?,?,?,?,?,?,?,?)');		
			$this->bindParam(1, $intAdType, PDO::PARAM_INT, 4);	
			$this->bindParam(2, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(3, $intOption, PDO::PARAM_INT, 4);
			$this->bindParam(4, $strKey, PDO::PARAM_STR, 50);
			$this->bindParam(5, $strStart, PDO::PARAM_STR, 10);
			$this->bindParam(6, $strEnd, PDO::PARAM_STR, 10);
			$this->bindParam(7, $intOff, PDO::PARAM_INT, 11);
			$this->bindParam(8, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult[0]['foundRows']) ? $arrResult[0]['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::getAll - ' .$e->getMessage());			
			
		}
	}
	public function get($intId) {
		try {						
			$this->prepare('call smanuals_get(?)');		
			$this->bindParam(1, $intId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			return isset($arrResult[0]) ? $arrResult[0] : null;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::get - ' .$e->getMessage());			
			
		}
	}
	public function add($intAdminId, $strTitle, $strShort, $strContent, $strImage, 
						$strFlash, $strLink, $intCategoryId=0, $intSubId=0, $strTag=''
						) {
		try {
			$this->prepare('call smanuals_add(?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strTitle, PDO::PARAM_STR, 255);
			$this->bindParam(3, $strShort, PDO::PARAM_STR, 500);
			$this->bindParam(4, $strContent, PDO::PARAM_STR);
			$this->bindParam(5, $strImage, PDO::PARAM_STR, 150);
			$this->bindParam(6, $strFlash, PDO::PARAM_STR);
			$this->bindParam(7, $strLink, PDO::PARAM_STR);
			$this->bindParam(8, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(9, $intSubId, PDO::PARAM_INT, 11);
			$this->bindParam(10, $strTag, PDO::PARAM_STR, 100);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$intId			= isset($arrResult[0]['id']) ? $arrResult[0]['id'] : 0;
				$objSearch		= new Ecore_Search();
				$strSlug		= Ecore_Rewrite_Smanual::slug($intId,$strTitle);
				$objSearch->addUpdate($intId, 3, $strTitle, '', '',
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 2, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::add - ' . $e->getMessage());
			
		}
	}	
	public function update($intAdminId, $intId, $strTitle, $strShort, $strContent, 
							$strImage, $strFlash, $strLink, $intCategoryId=0, $intSubId=0, 
							$strTag='') {
		try {
			$this->prepare('call smanuals_update(?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $strTitle, PDO::PARAM_STR, 255);
			$this->bindParam(4, $strShort, PDO::PARAM_STR, 500);
			$this->bindParam(5, $strContent, PDO::PARAM_STR);
			$this->bindParam(6, $strImage, PDO::PARAM_STR, 150);
			$this->bindParam(7, $strFlash, PDO::PARAM_STR);
			$this->bindParam(8, $strLink, PDO::PARAM_STR);
			$this->bindParam(9, $intCategoryId, PDO::PARAM_INT, 11);
			$this->bindParam(10, $intSubId, PDO::PARAM_INT, 11);
			$this->bindParam(11, $strTag, PDO::PARAM_STR, 100);
			$arrResult 	= $this->fetchAll();		
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strOldImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strOldImage);
				}
				$objSearch		= new Ecore_Search();
				$strSlug		= Ecore_Rewrite_Smanual::slug($intId,$strTitle);
				$objSearch->addUpdate($intId, 3, $strTitle, '', '',
									  $strShort, $strImage, $strSlug, 0, 0,
									  Ecore_Function::now(), 1, 2, $intCategoryId, $intSubId);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::update - ' . $e->getMessage());
			
		}
	}
	public function updateStatus($intAdminId, $intSmanualId, $intStatus, $intPerm) {
		try {				
			$this->prepare('call smanuals_update_status(?,?,?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intSmanualId, PDO::PARAM_INT, 11);
			$this->bindParam(3, $intStatus, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intPerm, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {
				$objSearch = new Ecore_Search();
				$objSearch->updateStatus($intSmanualId, 3, $intStatus);
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::updateStatus - ' .$e->getMessage());			
			
		}
	}
	public function delete($intAdminId, $intSmanualId) {
		try {			
			$this->prepare('call smanuals_delete(?,?)');
			$this->bindParam(1, $intAdminId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $intSmanualId, PDO::PARAM_INT, 11);
			$arrResult 	= $this->fetchAll();
			$intError	= isset($arrResult[0]['errCode']) ? $arrResult[0]['errCode'] : 0;
			if($intError == 0) {				
				$strImage = isset($arrResult[0]['image']) ? $arrResult[0]['image'] : '';
				if($strImage != '') {
					$this->deleteImg($strImage);
				}
			}
			return $intError;
		} catch(Exception $e) {
			Ecore_Function::logDb('Backend::Smanual::delete - ' .$e->getMessage());			
			
		}
	}
	public function deleteImg($strImg) {		
		if(file_exists(DIR_SHARE_FULL . $strImg))
			@unlink(DIR_SHARE_FULL . $strImg);
		if(file_exists(DIR_SHARE_THUMB . $strImg))
			@unlink(DIR_SHARE_THUMB . $strImg);
		if(file_exists(DIR_SHARE_SMALL . $strImg))
			@unlink(DIR_SHARE_SMALL . $strImg);
	}	
}